/**
 * 
 */
package vue.popup;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import model.session.ExecutionStatus;

import vue.session.IGExecutionBlock;
import controller.ISessionManager;

/**
 * A pop up menu for {@link IGExecutionBlock}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class JPopupExecution extends JPopupMenu implements IJPopupMenu, ActionListener {


	private static final long	serialVersionUID	= 1L;

	public static final String	TITLE_OUT			= "STD_OUT_";
	public static final String	TITLE_ERR			= "STD_ERR_";
	public static final String	ITEM_STOP			= "Stop Execution";
	public static final String	ITEM_OUT			= "Show stdout";
	public static final String	ITEM_ERR			= "Show stderr";
	public static final String	ITEM_REMOVE			= "Remove from session";


	protected JMenuItem			out;
	protected JMenuItem			err;
	// protected JMenuItem executeSolverItem;
	protected JMenuItem			remove;
	protected JMenuItem			stopItem;
	protected ISessionManager	manager;

	protected IGExecutionBlock	exec;

	/**
	 * Create a {@link JPopupExecution}.
	 * 
	 * @param exec
	 *            {@link IGExecutionBlock} associate.
	 * @param manager
	 *            the session manager.
	 */
	public JPopupExecution(IGExecutionBlock exec, ISessionManager manager) {
		this.manager = manager;
		this.exec = exec;
		createMenu();

		if (exec != null && exec.getExec().getStatus() == ExecutionStatus.RUNNING)
		{
			stopItem.setEnabled(true);
			remove.setEnabled(false);
		}
		else
		{
			stopItem.setEnabled(false);
			remove.setEnabled(true);
		}

		out.setEnabled(false);
		err.setEnabled(false);
	}


	/**
	 * Create the pop up menu for the execution.
	 */
	protected void createMenu() {
		// remove
		remove = new JMenuItem(ITEM_REMOVE);
		remove.addActionListener(this);

		add(remove);

		addSeparator();

		// stop
		stopItem = new JMenuItem(ITEM_STOP);
		stopItem.addActionListener(this);

		add(stopItem);

		addSeparator();

		// out
		out = new JMenuItem(ITEM_OUT);
		out.addActionListener(this);
		add(out);

		err = new JMenuItem(ITEM_ERR);
		err.addActionListener(this);
		add(err);
	}


	@Override
	public void refresh() {
		// TODO Auto-generated method stub
		if (exec != null && exec.getExec().getStatus() == ExecutionStatus.RUNNING)
		{
			stopItem.setEnabled(true);
			remove.setEnabled(false);
		}
		else
		{
			stopItem.setEnabled(false);
			remove.setEnabled(true);
		}

		// remove.setEnabled(false);
		out.setEnabled(false);
		err.setEnabled(false);
	}

	/**
	 * This method is call to show the stderr of the execution.
	 */
	public void showStdout() {
		new IGOutFrame(TITLE_OUT, getStdout());
	}

	/**
	 * This method is call to show the stderr of the execution.
	 */
	public void showStderr() {
		new IGOutFrame(TITLE_ERR, getStderr());
	}

	/**
	 * This method is call to remove the execution from the session.
	 */
	public void removeFromSession() {
	}


	/**
	 * You should override this method in order to return the stderr of the execution.
	 * 
	 * @return the stderr of the execution.
	 */
	public String getStderr() {
		return "";
	}


	/**
	 * You should override this method in order to return the stdout of the execution.
	 * 
	 * @return the stdout of the execution.
	 */
	public String getStdout() {
		return "";
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == out)
		{// show stdout
			showStdout();
			return;
		}
		if (e.getSource() == err)
		{// montre stderr
			showStderr();
			return;
		}
		if (e.getSource() == remove)
		{// montre stderr
			removeFromSession();
			return;
		}
		if (e.getSource() == stopItem)
		{// stop execution
			manager.stopExecution(exec.getCnfuse().getCnfuse(), exec.getExec());
			return;
		}
	}


}
